﻿using UnityEngine;

namespace LuaInterface
{
    /// <summary>
    /// 3d 碰撞器
    /// </summary>
    public class Lua3DCollider : MonoBehaviour
    {
        [Header("如果为null，则取当前gameObject上的LuaComponent")]
        public LuaComponent luaComponent;
        private void Start()
        {
            if(luaComponent==null) luaComponent = GetComponent<LuaComponent>();
        }

        void OnCollisionEnter(Collision collision)
        {
            if (luaComponent && luaComponent.luaTable != null)
            {
                LuaFunction func = luaComponent.luaTable.GetLuaFunction("OnCollisionEnter");
                if (func != null)
                {
                    func.Call(luaComponent.luaTable, collision);
                    func.Dispose();
                    func = null;
                }
            }
        }

        void OnCollisionExit(Collision collision)
        {
            if (luaComponent && luaComponent.luaTable != null)
            {
                LuaFunction func = luaComponent.luaTable.GetLuaFunction("OnCollisionExit");
                if (func != null)
                {
                    func.Call(luaComponent.luaTable, collision);
                    func.Dispose();
                    func = null;
                }
            }
        }

        void OnCollisionStay(Collision collision)
        {
            if (luaComponent && luaComponent.luaTable != null)
            {
                LuaFunction func = luaComponent.luaTable.GetLuaFunction("OnCollisionStay");
                if (func != null)
                {
                    func.Call(luaComponent.luaTable, collision);
                    func.Dispose();
                    func = null;
                }
            }
        }

        void OnTriggerEnter(Collider collider)
        {
            if (luaComponent && luaComponent.luaTable != null)
            {
                LuaFunction func = luaComponent.luaTable.GetLuaFunction("OnTriggerEnter");
                if (func != null)
                {
                    func.Call(luaComponent.luaTable, collider);
                    func.Dispose();
                    func = null;
                }
            }
        }

        void OnTriggerStay(Collider collider)
        {
            if (luaComponent && luaComponent.luaTable != null)
            {
                LuaFunction func = luaComponent.luaTable.GetLuaFunction("OnTriggerStay");
                if (func != null)
                {
                    func.Call(luaComponent.luaTable, collider);
                    func.Dispose();
                    func = null;
                }
            }
        }

        void OnTriggerExit(Collider collider)
        {
            if (luaComponent && luaComponent.luaTable != null)
            {
                LuaFunction func = luaComponent.luaTable.GetLuaFunction("OnTriggerExit");
                if (func != null)
                {
                    func.Call(luaComponent.luaTable, collider);
                    func.Dispose();
                    func = null;
                }
            }
        }

        void OnJointBreak(float breakForce)
        {
            if (luaComponent && luaComponent.luaTable != null)
            {
                LuaFunction func = luaComponent.luaTable.GetLuaFunction("OnJointBreak");
                if (func != null)
                {
                    func.Call(luaComponent.luaTable, breakForce);
                    func.Dispose();
                    func = null;
                }
            }
        }
    }
}